package com.sina.microblog.ut.cases
{
	import com.sina.microblog.MicroBlog;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	import com.sina.microblog.ut.utils.AssertResult;
	import com.sina.microblog.ut.utils.ResultDataUtils;
	import com.sina.microblog.ut.utils.URLRequestChecker;
	import com.sina.microblog.ut.utils.URLRequestParameters;
	
	import flash.net.URLRequest;
	
	import org.flexunit.async.Async;
	
	[RunWith("org.flexunit.experimental.theories.Theories")]
	public class UserInfoTest
	{
		private static const USER_SHOW_URL_BASE:String = "http://api.t.sina.com.cn/users/show";
		private static const STATUSES_FRIENDS_URL_BASE:String = "http://api.t.sina.com.cn/statuses/friends";
		private static const STATUSES_FOLLOWERS_URL_BASE:String = "http://api.t.sina.com.cn/statuses/followers";
		
		private static const TIME_OUT:uint = 500;	
		private static var mb:MicroBlog;
		private static var tester:String = "tester";
		
		[DataPoints]
		[ArrayElementType("int")]
		public static var ids:Array = [-1, 0, 1, 201, 300];
		
		public function UserInfoTest()
		{
		}
		
		[BeforeClass]
		public static function setUp():void
		{
			if ( null == mb )
			{
				mb = new MicroBlog();
			}
		}
		
		//Test UserInfo.
		[Test(async,timeout="20000")]
		public function testUserInfo():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = USER_SHOW_URL_BASE +"/tester.xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_USER_INFO_RESULT, AssertResult.assertUser, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadUserInfo("tester");
			URLRequestChecker.check(mb, req, ResultDataUtils.user);			
			
		}
		[Test(async,timeout="20000")]
		public function testUserFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = USER_SHOW_URL_BASE + "/tester.xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_USER_INFO_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadUserInfo("tester");
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Test(async,timeout="20000")]
		public function testUserInfoWithNullUser():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = USER_SHOW_URL_BASE + ".xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_USER_INFO_RESULT, AssertResult.assertUser, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadUserInfo();
			URLRequestChecker.check(mb, req, ResultDataUtils.user);			
			
		}
		[Test(async,timeout="20000")]
		public function testUserInfoWithEmptyUser():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = USER_SHOW_URL_BASE + ".xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_USER_INFO_RESULT, AssertResult.assertUser, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadUserInfo("");
			URLRequestChecker.check(mb, req, ResultDataUtils.user);			
			
		}
		//Test Statuses/friends.
		[Test(async,timeout="20000")]
		public function testFriends():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", 1111);
			var url:String = STATUSES_FRIENDS_URL_BASE + "/tester.xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FRIENDS_INFO_RESULT, AssertResult.assertUserArray, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsInfo(tester, 1111);
			URLRequestChecker.check(mb, req, ResultDataUtils.userArray);
			
		}
		[Test(async,timeout="20000")]
		public function testFriendsFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", 1111);
			var url:String = STATUSES_FRIENDS_URL_BASE + "/tester.xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_FRIENDS_INFO_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsInfo(tester, 1111);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);			
			
		}
		[Test(async,timeout="20000")]
		public function testFriendsWithNullUser():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", 1111);
			var url:String = STATUSES_FRIENDS_URL_BASE + ".xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FRIENDS_INFO_RESULT, AssertResult.assertUserArray, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsInfo(null, 1111);
			URLRequestChecker.check(mb, req, ResultDataUtils.userArray);
			
		}
		[Test(async,timeout="20000")]
		public function testFriendsWithEmptyUser():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", 1111);
			var url:String = STATUSES_FRIENDS_URL_BASE + ".xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FRIENDS_INFO_RESULT, AssertResult.assertUserArray, TIME_OUT);
			 
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsInfo("", 1111);
			URLRequestChecker.check(mb, req, ResultDataUtils.userArray);
			
		}
		[Theory]
		public function testFriendsTheory(cursor:int):void
		{	
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", 1111);
			if(cursor > -1 )
			{
				params.addParam("cursor", cursor);
			}
			var url:String = STATUSES_FRIENDS_URL_BASE + "/tester.xml" + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsInfo(tester, 1111, null, cursor);
			URLRequestChecker.check(mb, req, null);		
		}
		
		//Test Statuses/followers.
		[Test(async,timeout="20000")]
		public function testFollwers():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", 1111);
			var url:String = STATUSES_FOLLOWERS_URL_BASE + "/tester.xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FOLLOWERS_INFO_RESULT, AssertResult.assertUserArray, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFollowersInfo(tester, 1111);
			URLRequestChecker.check(mb, req, ResultDataUtils.userArray);
			
		}
		[Test(async,timeout="20000")]
		public function testFollowersFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", 1111);
			var url:String = STATUSES_FOLLOWERS_URL_BASE + "/tester.xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_FOLLOWERS_INFO_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFollowersInfo(tester, 1111);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);			
			
		}
		[Test(async,timeout="20000")]
		public function testFollwersWithNullUser():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", 1111);
			var url:String = STATUSES_FOLLOWERS_URL_BASE + ".xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FOLLOWERS_INFO_RESULT, AssertResult.assertUserArray, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFollowersInfo(null, 1111);
			URLRequestChecker.check(mb, req, ResultDataUtils.userArray);
			
		}
		[Test(async,timeout="20000")]
		public function testFollwersWithEmptyUser():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", 1111);
			var url:String = STATUSES_FOLLOWERS_URL_BASE + ".xml" + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FOLLOWERS_INFO_RESULT, AssertResult.assertUserArray, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFollowersInfo("", 1111);
			URLRequestChecker.check(mb, req, ResultDataUtils.userArray);
			
		}
		[Theory]
		public function testFollowersTheory(cursor:int):void
		{	
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("user_id", 1111);
			if(cursor > -1 )
			{
				params.addParam("cursor", cursor);
			}
			var url:String = STATUSES_FOLLOWERS_URL_BASE + "/tester.xml" + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadFollowersInfo(tester, 1111, null, cursor);
			URLRequestChecker.check(mb, req, null);		
		}
	}
}